package com.zyw.zhuangdian.service.impl;

import com.zyw.zhuangdian.constant.ServiceConstant;
import com.zyw.zhuangdian.dao.UserDao;
import com.zyw.zhuangdian.pojo.dto.PageDto;
import com.zyw.zhuangdian.property.MyProperties;
import com.zyw.zhuangdian.service.UserService;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.in.UserDtoIn;
import com.zyw.zhuangdian.pojo.out.UserDtoOut;
import com.zyw.zhuangdian.util.Base64Util;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import org.apache.commons.lang3.StringUtils;
import org.mindrot.jbcrypt.BCrypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    private final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserDao userDao;

    @Autowired
    private MyProperties properties;

    @Override
    public Resp<Void> addUser(UserDtoIn in) {
        Resp<Void> resp = new Resp<>();

        in.setUid(Base64Util.newUUID());
        in.setSalt(BCrypt.gensalt());
        in.setPassword(BCrypt.hashpw(in.getPassword(), in.getSalt()));

        userDao.addUser(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> updateUser(UserDtoIn in) {
        Resp<Void> resp = new Resp<>();

        if (StringUtils.isNotBlank(in.getPassword())) {
            in.setSalt(BCrypt.gensalt());
            in.setPassword(BCrypt.hashpw(in.getPassword(), in.getSalt()));
        }

        userDao.updateUser(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> deleteUser(UserDtoIn in) {
        Resp<Void> resp = new Resp<>();

        in.setStatus(ServiceConstant.STATUS_INVALID);

        userDao.updateUser(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<List<UserDtoOut>> listUsers(UserDtoIn in) {
        Resp<List<UserDtoOut>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }

        int count = userDao.listUsersCount(BeanConvertUtil.beanCovertMap(in));
        if (count > 0) {
            List<Map> users = userDao.listUsers(BeanConvertUtil.beanCovertMap(in));
            resp.setData(BeanConvertUtil.covertBeanList(users, UserDtoOut.class));
        }else{
            resp.setData(new ArrayList<>());
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }

        return resp;
    }

    @Override
    public Resp<List<UserDtoOut>> listUsersForLogin(UserDtoIn in) {
        Resp<List<UserDtoOut>> resp = new Resp<>();

        int count = userDao.listUsersCountForLogin(BeanConvertUtil.beanCovertMap(in));
        if (count > 0) {
            List<Map> users = userDao.listUsersForLogin(BeanConvertUtil.beanCovertMap(in));
            resp.setData(BeanConvertUtil.covertBeanList(users, UserDtoOut.class));
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }

        return resp;
    }
}
